#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->label->setText("输入表达式:");
    ui->label_2->setText("结果:");
    ui->label_3->setText("输入表达式:");
    ui->label_4->setText("合法的表达式形如x+2x^2+40x^11,或2y^10+40y^5+1");
    ui->AddButton->setText(QString("加法计算"));
    ui->MultButton->setText(QString("乘法计算"));
    ui->ClearButton->setText(QString("清空"));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_InputText1_textEdited(const QString &arg1)
{
    if (ui->InputText1->text().length() >= 100)
    {
        ui->OutputText->setText(QString("表达式过长,请重新输入!"));
        ui->AddButton->setEnabled(false);
    }
    else
    {
        if (ui->InputText2->text().length() < 100)
        {
            ui->AddButton->setEnabled(true);
            if (ui->OutputText->text() == "表达式过长,请重新输入!")
            {
                ui->OutputText->setText("");
            }
        }
        strcpy(data.input1, ui->InputText1->text().toLatin1().data());
    }
}

void MainWindow::on_ClearButton_clicked()
{
    ui->InputText1->setText("");
    ui->InputText2->setText("");
    ui->OutputText->setText("");
    ui->AddButton->setEnabled(true);
}

void MainWindow::on_AddButton_clicked()
{
    Status status = AddExpression(&data);
    if (status == FINISH)
    {
        ui->OutputText->setText(QString(data.output));
    }
    else
    {
        ui->OutputText->setText(QString("表达式输入有误!"));
    }
}

void MainWindow::on_InputText2_textEdited(const QString &arg1)
{
    if (ui->InputText2->text().length() >= 100)
    {
        ui->OutputText->setText(QString("表达式过长,请重新输入!"));
        ui->AddButton->setEnabled(false);
    }
    else
    {
        if (ui->InputText1->text().length() < 100)
        {
            ui->AddButton->setEnabled(true);
            if (ui->OutputText->text() == "表达式过长,请重新输入!")
            {
                ui->OutputText->setText("");
            }
        }
        strcpy(data.input2, ui->InputText2->text().toLatin1().data());
    }
}

void MainWindow::on_MultButton_clicked()
{
    Status status = MultExpression(&data);
    if (status == FINISH)
    {
        ui->OutputText->setText(QString(data.output));
    }
    else
    {
        ui->OutputText->setText(QString("表达式输入有误!"));
    }
}
